package com.meirengu.webview.service.impl;

import com.meirengu.common.RedisClient;
import com.meirengu.service.impl.BaseServiceImpl;
import com.meirengu.utils.DateUtils;
import com.meirengu.webview.common.Constants;
import com.meirengu.webview.common.UserChannelEnum;
import com.meirengu.webview.dao.KwInterfaceConfigDao;
import com.meirengu.webview.model.KwHnbFilter;
import com.meirengu.webview.model.KwInterfaceConfig;
import com.meirengu.webview.model.User;
import com.meirengu.webview.model.UserInfo;
import com.meirengu.webview.service.KwHnbFilterService;
import com.meirengu.webview.service.KwHnbPushService;
import com.meirengu.webview.service.UserInfoService;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * KwHnbFilter服务实现层
 *
 * @author Mao
 * @create Wed May 23 18:15:51 CST 2018
 */
@Service
public class KwHnbFilterServiceImpl extends BaseServiceImpl<KwHnbFilter> implements KwHnbFilterService {

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private KwInterfaceConfigDao kwInterfaceConfigDao;

    @Autowired
    private KwHnbPushService kwHnbPushService;


    Logger logger = LoggerFactory.getLogger(KwHnbFilterServiceImpl.class);


    /**
     * 发送佳佳融接口
     * 过滤条件
     * 1年龄 28-50
     * 2城市
     * 3贷款额度大于5w
     * 4信用卡必须有
     * 5房车寿3其一
     *
     * @param userInfo
     * @param user
     * @throws Exception
     */
    public boolean sendJjr(UserInfo userInfo, User user) throws Exception {
        boolean sendFlag = false;
        try {
            String jjrIol = redisClient.get("iol-jjr");
            if ("N".equals(jjrIol)) {
                String id = user.getId();
                String name = user.getName();
                String mobile = user.getMobile();
                Integer sex = user.getSex();
                int age = 25;
                Date brithdayDate = userInfo.getBirthday();
                age = DateUtils.getAge(brithdayDate);
                String city = userInfo.getScity();

                BigDecimal expectAmount = userInfo.getExpectAmount();
                Integer house = userInfo.getHouse();
                Integer car = userInfo.getCar();
                Integer fund = userInfo.getFund();
                Integer lifeInsurance = userInfo.getPolicy();
                Integer wxloan = userInfo.getWxloan();
                Integer salary = userInfo.getSalary();
                Integer worklong = userInfo.getWorkDuration();
                Integer socialSecurity = userInfo.getSocialSecurity();
                Integer credit = userInfo.getCredit();
                String ip = userInfo.getIp();

                KwHnbFilter kwHnbFilter = new KwHnbFilter();
                kwHnbFilter.setUserId(id);
                kwHnbFilter.setMobile(mobile);
                kwHnbFilter.setChannelCode("jjr");
                kwHnbFilter.setChannelName("佳佳融");

                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("jjr");

                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwHnbFilter.setSuited("N");
                            kwHnbFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwHnbFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwHnbFilter.setSuited("N");
                            kwHnbFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwHnbFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (kwInterfaceConfig.getMinAmount() != null && expectAmount.intValue() < kwInterfaceConfig.getMinAmount()
                                || kwInterfaceConfig.getMaxAmount() != null && expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwHnbFilter.setSuited("N");
                            kwHnbFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwHnbFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitHouse())) {
                        if (!kwInterfaceConfig.getHouseValue().contains(house.intValue() + "")) {
                            dataFlag = false;
                            kwHnbFilter.setSuited("N");
                            kwHnbFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwHnbFilter.setRefuseMsg("没有房：" + house.intValue());
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitCar())) {
                        if (!kwInterfaceConfig.getCarValue().contains(car.intValue() + "")) {
                            dataFlag = false;
                            kwHnbFilter.setSuited("N");
                            kwHnbFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwHnbFilter.setRefuseMsg("没有车：" + car.intValue());
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitPolicy())) {
                        if (!kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "")) {
                            dataFlag = false;
                            kwHnbFilter.setSuited("N");
                            kwHnbFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwHnbFilter.setRefuseMsg("没有寿险：" + lifeInsurance.intValue());
                        }
                    }

                    if (dataFlag) {
                        //满足条件之一就行
                        if (kwInterfaceConfig.getSocialSecurityValue().contains(socialSecurity.intValue() + "") ||
                                kwInterfaceConfig.getFundValue().contains(String.valueOf(fund)) ||
                                kwInterfaceConfig.getHouseValue().contains(house.intValue() + "") ||
                                kwInterfaceConfig.getCarValue().contains(car.intValue() + "") ||
                                kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "") ||
                                kwInterfaceConfig.getWxloanValue().contains(wxloan.intValue() + "")) {

                            Integer sexVal = 1;
                            if (sex.intValue() == 1) {
                                sexVal = 2;
                            }

                            String birthdayStr = DateUtils.dateToStr(brithdayDate, "yyyy-MM-dd");
                            int loanAmount = (expectAmount.divide(new BigDecimal(10000))).intValue();

                            //房子
                            int houseVal;
                            if (house.intValue() == 1) {
                                houseVal = 3;
                            } else if (house.intValue() == 2) {
                                houseVal = 2;
                            } else {
                                houseVal = 1;
                            }

                            //车子
                            int carVal;
                            if (car.intValue() == 1) {
                                carVal = 3;
                            } else if (car.intValue() == 2) {
                                carVal = 2;
                            } else {
                                carVal = 1;
                            }

                            //保单
                            int policyVal = 1;
                            if (lifeInsurance.intValue() == 1) {
                                policyVal = 2;
                            }

                            //公积金
                            int fundVal = 1;
                            if (fund.intValue() == 1) {
                                fundVal = 2;
                            } else if (fund.intValue() == 2) {
                                fundVal = 3;
                            }

                            //社保
                            int socialSecurityVal = 1;
                            if (socialSecurity.intValue() == 1) {
                                socialSecurityVal = 2;
                            } else if (socialSecurity.intValue() == 2) {
                                socialSecurityVal = 3;
                            }

                            //微粒贷
                            int weiVal = 1;
                            if (wxloan == 1) {
                                weiVal = 3;
                            }

                            //信用卡
                            int creditVal = 2;
                            if (credit == 1) {
                                carVal = 1;
                            }

                            //工作时间
                            int workTime = 1;
                            if (worklong == 12) {
                                workTime = 2;
                            } else if (worklong == 24) {
                                workTime = 3;
                            }

                            //工资
                            int salaryVal = 1;
                            if (salary == 5) {
                                salaryVal = 3;
                            } else if (salary == 3 || salary == 4) {
                                salaryVal = 2;
                            }

                            Integer code = kwHnbPushService.applyJjr(name, age, birthdayStr, sexVal, mobile, ip
                                    , city, loanAmount, houseVal, carVal, policyVal, fundVal, socialSecurityVal, weiVal, creditVal, workTime, salaryVal);
                            kwHnbFilter.setSuited("Y");
                            kwHnbFilter.setCode(UserChannelEnum.PASS.getValue());
                            /*推送接口后修改限制数量*/
                            userInfoService.updateLimitNum(code, "jjr", user);
                            if (code.intValue() == Constants.BUSINESS_STATUS_SUCCESS) {
                                sendFlag = true;
                            }
                        } else {
                            kwHnbFilter.setSuited("N");
                            kwHnbFilter.setCode(UserChannelEnum.FAIL_FUND.getValue());
                            kwHnbFilter.setRefuseMsg("用户无公积金：" + fund + " 无保单：" + lifeInsurance + " 无房：" + house + " 无车：" + car);
                        }
                    }
                }

                insert(kwHnbFilter);
            }
        } catch (
                Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }



}
